package com.hgkj.cloud.customer.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public final class DateTimeUtil {
	// a integer to xx:xx:xx
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	/*出生年月日类似1985-02-06*/
	public static int getAgeByBirthday(String pDate) {
		Calendar cal = Calendar.getInstance();

		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH)+1;
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);

		int yearBirth = Integer.valueOf(pDate.substring(0, 4));
		int monthBirth = Integer.valueOf(pDate.substring(5, 7));
		int dayOfMonthBirth = Integer.valueOf(pDate.substring(8, 10));

		int age = yearNow - yearBirth;

		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				//monthNow==monthBirth
				if (dayOfMonthNow < dayOfMonthBirth) {
					age--;
				}
			} else {
				//monthNow>monthBirth
				age--;
			}
		}
		return age;
	}
		
	public static Float getFloatAgeByBirthday(Long birth) {
		String pDate = getWholeStringFromUTC(birth);
		Calendar cal = Calendar.getInstance();

		Integer yearNow = cal.get(Calendar.YEAR);
		Integer monthNow = cal.get(Calendar.MONTH)+1;

		Integer yearBirth = Integer.valueOf(pDate.substring(0, 4));
		Integer monthBirth = Integer.valueOf(pDate.substring(5, 7));

		Integer age = yearNow - yearBirth;
		Float shift = (monthNow.floatValue() - monthBirth.floatValue())/12;		
		return age + shift;
	}

	/*日期转UTC时间，返回秒数(准备弃用)*/
	public static Long getUtcFromString(String str){
		Long seconds = 0L;
		if(str!=null && !str.equals("")){
			String tpTme = str+" 00:00:00";
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time;
			try {
				time = df.parse(tpTme);
				long sec = time.getTime();
				seconds = (Long)(sec/1000);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return seconds;
	}
	
	/*日期转UTC时间，返回秒数*/
	public static Long getUtcFromStrings(String str){
		Long seconds = 0L;
		if(str!=null && !str.equals("")){
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time;
			try {
				time = df.parse(str);
				seconds = time.getTime();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return seconds;
	}
	
	/*结束日期转UTC时间，返回秒数*/
	public static Long getEndUtcFromString(String str){
		Long seconds = 0L;
		if(str!=null && !str.equals("")){
			String tpTme = str+" 23:59:59";
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time;
			try {
				time = df.parse(tpTme);
				long sec = time.getTime();
				seconds = (Long)(sec/1000);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return seconds;
	}

	public static Long getStartTimeStemp(String str){
		Long timeStemp = 0L;
		if(str!=null && !str.equals("")){
			String tpTme = str + " 00:00:00";
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time;
			try {
				time = df.parse(tpTme);
				timeStemp = time.getTime();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return timeStemp;
	}

	public static Long getEndTimeStemp(String str){
		Long timeStemp = 0L;
		if(str!=null && !str.equals("")){
			String tpTme = str+" 23:59:59";
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date time;
			try {
				time = df.parse(tpTme);
				timeStemp = time.getTime();
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return timeStemp;
	}

	/*UTC时间转日期时间*/
	public static String getWholeStringFromUTC(Long seconds){
		String dateStr = "";
		long temp = (long)seconds*1000;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = new Date(temp);
		dateStr = df.format(time);
		return dateStr;
	}
	/*UTC时间转日期时间*/
	public static String getTimeStringFromUTC(Long seconds){
		String dateStr = "";
		long temp = (long)seconds*1000;
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		Date time = new Date(temp);
		dateStr = df.format(time);
		return dateStr;
	}

	/*UTC时间转日期*/
	public static String getStringFromUTC(Long seconds){
		String dateStr = "";
		long temp = (long)seconds*1000;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = new Date(temp);
		dateStr = df.format(time);
		return dateStr.substring(0,10);
	}

	/*UTC时间转时间*/
	public static String getTimeStringFromUTC(int seconds){
		String timeStr = "";
		long temp = (long)seconds*1000;;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date time = new Date(temp);
		timeStr = df.format(time);
		return timeStr.substring(11);
	}

	/*日期换算周几*/
	public static String getWeekOfDate(String dateStr) {
		String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
		Date date= null;
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
			date = sdf.parse(dateStr);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
			return weekDaysName[intWeek];
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	/*秒数转换为字符串*/
	public static String getStringFromSeconds(Integer seconds){
		int hour = seconds/(3600);
		int minute = seconds%(3600)/60;
		int second = seconds%(3600)%60;		
		return String.format("%02d:%02d:%02d", hour,minute, second);
	}
	
	/*获取当前时间字符串*/
	public static String getNowString(){
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
		String datetime = tempDate.format(new Date());  	
		return datetime;
	}
	
	/*判断当前时间是否在区间内*/
	public static boolean isBetween(Date sDate,Date eDate){
		if(sDate == null || eDate == null)
			return false;
		Date now = new Date(); 
		if(now.getTime() > sDate.getTime() && now.getTime() < eDate.getTime())
			return true;
		return false;
	}

	public static String dateToString(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String s = sdf.format(date);
		return s;
	}
}
